package com.test05_数组应用;

/**
 * 一对多关联映射
 */
public class YootkDemo_03 {

    public static void main(String[] args) {
        // 声明并实例化各自的类对象
        Item item = new Item(1, "编程语言");
        Subitem subA = new Subitem(10, "Java编程语言");
        Subitem subB = new Subitem(11, "Python编程语言");
        Subitem subC = new Subitem(12, "GO编程语言");
        Course courseA = new Course(10001, "Java从入门到项目实战", 99.8);
        Course courseB = new Course(10002, "JavaWEB开发", 299.8);
        Course courseC = new Course(10003, "Python从入门到项目实战", 99.8);
        Course courseD = new Course(10004, "Python高手编程", 99.8);
        // 设置彼此之间的引用关联
        item.setSubitems(new Subitem[]{subA, subB, subC});
        item.setCourses(new Course[]{courseA, courseB, courseC, courseD});
        subA.setCourses(new Course[]{courseA, courseB});
        subB.setCourses(new Course[]{courseC, courseD});
        courseA.setItem(item);
        courseB.setItem(item);
        courseC.setItem(item);
        courseD.setItem(item);
        courseA.setSubitem(subA);
        courseB.setSubitem(subA);
        courseC.setSubitem(subB);
        courseD.setSubitem(subB);
        // 依据引用结构获取相关的数据信息
        System.out.println(item.getInfo());
        System.out.println("--------------------- 根据分类找到子分类数据 ---------------------");
        for (int x = 0; x < item.getSubitems().length; x++) {
            System.out.println("\t|- " + item.getSubitems()[x].getInfo());
        }
        System.out.println("--------------------- 根据分类找到课程数据 ---------------------");
        for (int x = 0; x < item.getCourses().length; x++) {
            System.out.println("\t|- " + item.getCourses()[x].getInfo());
        }
        System.out.println("--------------------- 根据子分类找到课程数据 ---------------------");
        for (int x = 0; x < subA.getCourses().length; x++) {
            System.out.println("\t|- " + subA.getCourses()[x].getInfo());
        }
        System.out.println("--------------------- 根据课程找到对应的分类信息 ---------------------");
        System.out.println(courseA.getInfo());
        System.out.println("\t|- " + courseA.getItem().getInfo());
        System.out.println("\t|- " + courseA.getSubitem().getInfo());
    }
}

/**
 * 分类
 */
class Item {
    private long iid;
    private String title;
    private Subitem subitems[];  // 子分类
    private Course courses[];    // 多个课程

    public Item() {
    }

    public Item(long iid, String title) {
        this.iid = iid;
        this.title = title;
    }

    public void setSubitems(Subitem subitems[]) {
        this.subitems = subitems;
    }

    public Subitem[] getSubitems() {
        return this.subitems;
    }

    public void setCourses(Course courses[]) {
        this.courses = courses;
    }

    public Course[] getCourses() {
        return this.courses;
    }

    public String getInfo() {
        return "【Item】分类ID：" + this.iid + "、分类标题：" + this.title;
    }
}

/**
 * 子分类
 */
class Subitem {
    private long sid;
    private String title;
    private Course courses[];   // 多个课程

    public Subitem() {
    }

    public Subitem(long sid, String title) {
        this.sid = sid;
        this.title = title;
    }

    public void setCourses(Course courses[]) {
        this.courses = courses;
    }

    public Course[] getCourses() {
        return this.courses;
    }

    public String getInfo() {
        return "【Subitem】子分类ID：" + this.sid + "、子分类标题：" + this.title;
    }
}

/**
 * 课程
 */
class Course {
    private long cid;
    private String name;
    private double price;
    private Item item;          // 对应分类
    private Subitem subitem;    // 对应子分类

    public Course(long cid, String name, double price) {
        this.cid = cid;
        this.name = name;
        this.price = price;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Item getItem() {
        return this.item;
    }

    public void setSubitem(Subitem subitem) {
        this.subitem = subitem;
    }

    public Subitem getSubitem() {
        return this.subitem;
    }

    public String getInfo() {
        return "【Course】课程ID：" + this.cid + "、课程名称：" + this.name + "、课程价格：" + this.price;
    }
}
